<template>
  <div>
    <BasicTable @register="registerTable" @resize-column="handleResizeColumn">
      <template #toolbar> </template>

      <template #status="{ column, record }">
        <Select style="width: 100%" :options="statusList" v-model:value="record[column.key]" />
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'ant-design:check-outlined',
              tooltip: '确定',
              onClick: handleSure.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              tooltip: '清除用户',
              onClick: handleDelete.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref, h, nextTick, watchEffect } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Select } from 'ant-design-vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  import { columns, searchFormSchema, statusList } from './nodeTable.data';
  import {
    getFlowGetActions,
    getFlowChangeActionStatus,
    getFlowClearActionUser,
  } from '/@/api/flowManage/flowManage';
  import { getListCard } from '/@/api/system/system';

  export default defineComponent({
    components: { BasicTable, TableAction, Select },
    props: {
      typeCode: {
        type: String,
        default: '',
      },
      flowId: {
        type: [String, Number],
        default: '',
      },
    },
    setup(props, { emit }) {
      const { createMessage, createConfirm } = useMessage();

      watchEffect(async () => {
        await nextTick();
        if (props.flowId) {
          const data = await getFlowGetActions({ flowId: props.flowId });
          setTableData(data);
        } else {
          setTableData([]);
        }
      });

      const [registerTable, { reload, setTableData, deleteTableDataRecord }] = useTable({
        title: '',
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        searchInfo: {
          op: 'search',
        }, //额外的参数
        useSearchForm: false,
        showTableSetting: false,
        bordered: true,
        indexColumnProps: { width: 50 },
        showIndexColumn: true,
        beforeFetch: (info) => {
          let newInfo = info;
          if (newInfo.dates && newInfo.dates.length > 0) {
            newInfo.fromDate = newInfo.dates[0];
            newInfo.toDate = newInfo.dates[1];
            delete newInfo.dates;
          }
          return newInfo;
        },
        immediate: false,
        pagination: false,
        canResize: false,
        actionColumn: {
          width: 150,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
        rowKey: 'id',
      });

      //清除用户
      async function handleDelete(record: Recordable) {
        createConfirm({
          iconType: 'warning',
          title: () => h('span', t('common.prompt')),
          content: () => h('span', '您确定要执行么'),
          onOk: async () => {
            const params = {
              actionId: record.id,
            };
            await getFlowClearActionUser(params);
            createMessage.success('操作成功');
            deleteTableDataRecord(record.id);
            // emit('nodeTableSuccess');
          },
        });
      }

      //确定
      function handleSure(record: any) {
        createConfirm({
          iconType: 'warning',
          title: () => h('span', t('common.prompt')),
          content: () => h('span', '您确定要执行么'),
          onOk: async () => {
            const params = {
              actionId: record.id,
              status: record.status,
            };
            await getFlowChangeActionStatus(params);
            createMessage.success('操作成功');
            // emit('nodeTableSuccess');
          },
        });
      }

      function handleSuccess() {
        reload();
      }
      onMounted(() => {});
      return {
        registerTable,
        handleDelete,
        handleSure,
        handleResizeColumn: (w, col) => {
          col.width = w;
        },
        statusList,
      };
    },
  });
</script>
